package com.dituhui.web.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.dituhui.gps.service.GetTrack;
import com.dituhui.gps.vo.GPSLine;
import com.dituhui.tool.io.Constants;
import com.dituhui.tool.util.DateUtil;
import com.dituhui.tool.util.StringUtil;
import com.dituhui.web.util.UserInfoUtil;

public class Track extends BaseService {
	private static Logger LOGGER = Logger.getLogger(Track.class);
	private String redisUrl;
	private int redisPort;
	private String redisPwd;
	private String historyESUrl;

	public void init() throws ServletException {
		redisUrl = Constants.getValue("REDIS.HOST", "gps_config.properties");
		redisPort = Integer.parseInt(Constants.getValue("REDIS.PORT", "gps_config.properties"));
		redisPwd = Constants.getValue("REDIS.PWD", "gps_config.properties");
		LOGGER.info("redisUrl=" + redisUrl);
		this.historyESUrl = Constants.getValue("HistoryESUrl", "gps_config.properties");
		LOGGER.info("historyESUrl=" + this.historyESUrl);
	}

	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		super.service(request, response);
		String groupId = request.getParameter("groupId");
		// LOGGER.info("groupId = " + groupId);
		boolean isGroupIdValid = UserInfoUtil.isGroupIdValid(groupId);
		StringBuilder output = new StringBuilder();
		if (!isGroupIdValid) {
			output.append("groupId非法");
		} else {
			String accountId = request.getParameter("accountId");
			String strDay = request.getParameter("day");
			Date todayDate = new Date();
			SimpleDateFormat dayFormat = DateUtil.getDayFormat();
			if (strDay == null || strDay.length() == 0) {
				strDay = dayFormat.format(todayDate);
			}
			if (accountId == null) {
				output.append("参数a非法，应传入accountId");
			} else {
				// 当天日期
				String strTodayDate = dayFormat.format(todayDate);
				String gpsData = "";

				// 是否需要查询当天
				boolean isQueryToday = strTodayDate.equals(strDay);
				// System.out.println("isQueryToday =" + isQueryToday);

				if (!isQueryToday) {
					// 查询历史
					try {
						Map<String, String> datas = GetTrack.getGPSLineFromHistoryES(accountId, groupId, strDay, strDay, this.historyESUrl);
						if (datas != null) {
							gpsData = datas.get(strDay);
						}
					} catch (Exception e) {
						String message = "seacrhHistory发生异常。";
						LOGGER.warn(message, e);
						// e.printStackTrace();
					}
					// System.out.println(gpsData);
				} else {
					// 查询当天
					try {
						boolean sortByTime = true;
						GPSLine gpsLine = GetTrack.getGPSLineFromRedis(redisUrl, redisPort, redisPwd, accountId, groupId, sortByTime);
						if (gpsLine != null) {
							gpsData = gpsLine.toCSVString();
							// LOGGER.info("(getGPSLineFromRedis)gpsLine=" + gpsLine.toCSVString());
						}
					} catch (Exception e) {
						String message = "seacrhRealtime发生异常。";
						LOGGER.warn(message, e);
						// e.printStackTrace();
					}
				}

				if (gpsData != null && gpsData.length() > 0) {
					// 只有一条记录
					output.append(strDay + StringUtil.AngularBracket + gpsData);
				} else {
					output.append("NoData");
				}
			}
		}
		PrintWriter out = response.getWriter();
		out.print(output.toString());
		out.flush();
		out.close();
	}
}
